/*
 * @Author: your name
 * @Date: 2020-12-21 14:19:38
 * @LastEditTime: 2021-01-13 11:01:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /wzry-auto-js/_click.js
 */
module.exports = {

    // 所有辅助脚本初始化的操作
    init: function (operationName) {
        // 当前所有脚本中的坐标都是基于小米 9（2340 X 1080）的屏幕适配，通过调用该函数，可实现在其他分辨率的设备上自动进行坐标缩放
        setScreenMetrics(1080, 2340);
        // 确保无障碍服务已启用
        auto.waitFor();
        this.log("即将开始运行 " + operationName);
    },
    // -- 对常用函数的一些封装 --
    launchPackage: function (msg, packageName) {
        this.log(msg)
        launchPackage(packageName)
        this.sleep_log()
        this.sleep("额外等待一会", 5)
    },
    // 在命令行和toast输出日志
    log: function log(msg) {
        console.info(msg)
        toast(msg)
    },
    //操作控件
    press: function(msg,x,y){
        this.log(msg)
        //模拟按住坐标 点击位置(x, y), 每次用时200毫秒
        press(x, y,200)
    },
    swipe: function (msg, x1, y1, x2, y2, duration) {
        this.log(msg)
        this.sml_move(x1, y1, x2, y2, duration)
        this.sleep_log()
    },
    // 通用等待逻辑
    sleep: function (msg, seconds) {
        this.log(msg)
        sleep(seconds * 1000)
    },
    //操作后，等待时间
    sleep_log: function(){
        //1000 = 1秒
        sleep(5000)
    },
    //返回,等待加载
    back_sleep: function(){
        back();
        sleep(3000)
    },
    // 退出到最顶层
    back_to_top: function (msg, maxLevel, waitMillSeconds) {
        this.log(msg)
        for (let i = 0; i < maxLevel; i++) {
            back()
            sleep(waitMillSeconds)
        }
    },
    // 双击退出当前应用
    exit_current_app: function (msg) {
        this.log(msg)
        // 双击back，退出当前应用
        back()
        back()
        back()
    },

    //关闭微信 内置网页浏览界面
    VxClose:function(){
        this.findByUiObjer("android.widget.ImageView","drawingOrder1",[4,1],"press");
        // className("android.widget.LinearLayout").clickable(true).depth(9).findOnce().click();
    },

    ViewGroupClick: function(str){
        var str1 = className("android.view.ViewGroup").desc(str).find();
        if (str1.nonEmpty()) {
            this.log("点击-"+str1);
            str1.click();
            this.sleep_log();
        }else{
            console.log('未找到'+str1);
        }
    },
    ImageView: function(str){
        var str1 = className("android.widget.ImageView").desc(str).find();
        if (str1.nonEmpty()) {
            this.log("点击-"+str1);
            str1.click();
            this.sleep_log();
        }else{
            console.log('未找到'+str1);
        }
    },
    RelativeLayout: function(str){
        var str1 = className("android.widget.RelativeLayout").desc(str).find();
        if (str1.nonEmpty()) {
            this.log("点击-"+str1);
            str1.click();
            this.sleep_log();
        }else{
            console.log('未找到'+str1);
        }
    },
    ViewDepthText: function(str,index){
        var str1 = className("android.view.View").depth(index).text(str).find();
        if (str1.nonEmpty()) {
            console.log('找到'+str1);
            str1.click();
            this.sleep_log();
        }else{
            console.log('未找到'+str1);
        }
    },
    ImgageDepthDesc: function(str,index){
        var str1 = className("android.widget.ImageView").depth(index).desc(str).find();
        if (str1.nonEmpty()) {
            console.log('找到'+str1);
            str1.click();
            this.sleep_log();
        }else{
            console.log('未找到'+str1);
        }
    },
    viewView: function(str){
        var str1 = className("android.view.View").text(str).findOne(1000);
        // this.log('找到'+str1);
        this.press("点击-"+str,str1.bounds().centerX(),str1.bounds().centerY())
    },
    widgetTextView: function(str){
        var str1 = className("android.widget.TextView").text(str).findOne(1000);
        // this.log('找到'+str1);
        this.press("点击-"+str,str1.bounds().centerX(),str1.bounds().centerY())
    },
    findByUiObjer: function(UiClassName,control,str,clickType){
        let str1;
        switch (control) {
            case "id":
                str1 = className(UiClassName).id(str).findOnce()
                break;
            case "text":
                str1 = className(UiClassName).text(str).findOnce()
                break;
            case "desc":
                str1 = className(UiClassName).desc(str).find()
                break;
            case "drawingOrder":
                this.log("找到："+UiClassName);
                str1 = className(UiClassName).drawingOrder(str).findOnce();
                break;
            case "drawingOrder1":
                this.log("找到："+UiClassName);
                str1 = className(UiClassName).depth(str[0]).drawingOrder(str[1]).findOnce();
                break;
            case "drawingOrder_click":
                this.log("找到："+UiClassName);
                str1 = className(UiClassName).depth(str[0]).drawingOrder(str[1]).clickable(true).findOnce();
                break;
            case "EditText":
                this.log("找到："+UiClassName);
                str1 = className(UiClassName).clickable(str[0]).depth(str[1]).findOnce();
                break;
            default:
                this.log("未找到"+UiClassName+"控件");
                break;
        }
        switch (clickType) {
            case "click":
                this.log("操作：click");
                // this.log(str1);
                str1.click();
                this.sleep_log();
                break;
            case "press":
                this.log("操作：press");
                // this.log(str1);
                this.press("点击："+str,str1.bounds().centerX(),str1.bounds().centerY())
                this.sleep_log();
                break;
            case "setText":
                str1.setText(str[2]);
                this.sleep_log();
                break;
            default:
                this.log("未找到点击方式");
                break;
        }
        
    },
    //长距离测试
    // sml_move(400, 1800, 800, 230, 1000);
    //短距离测试
    //sml_move(400, 1000, 800, 600, 1000);
    bezier_curves: function(cp, t) {
        cx = 3.0 * (cp[1].x - cp[0].x); 
        bx = 3.0 * (cp[2].x - cp[1].x) - cx; 
        ax = cp[3].x - cp[0].x - cx - bx; 
        cy = 3.0 * (cp[1].y - cp[0].y); 
        by = 3.0 * (cp[2].y - cp[1].y) - cy; 
        ay = cp[3].y - cp[0].y - cy - by; 
        
        tSquared = t * t; 
        tCubed = tSquared * t; 
        result = {
            "x": 0,
            "y": 0
        };
        result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; 
        result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; 
        return result; 
    },
    //仿真随机带曲线滑动  
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
    sml_move: function(qx, qy, zx, zy, time) {
        var xxy = [time];
        var point = [];
        var dx0 = {
            "x": qx,
            "y": qy
        };

        var dx1 = {
            "x": random(qx - 100, qx + 100),
            "y": random(qy , qy + 50)
        };
        var dx2 = {
            "x": random(zx - 100, zx + 100),
            "y": random(zy , zy + 50),
        };
        var dx3 = {
            "x": zx,
            "y": zy
        };
        for (var i = 0; i < 4; i++) {
            eval("point.push(dx" + i + ")");
        };
        log(point[3].x)
        for (let i = 0; i < 1; i += 0.08) {
            xxyy = [parseInt(this.bezier_curves(point, i).x), parseInt(this.bezier_curves(point, i).y)]
            xxy.push(xxyy);
        }
        log(xxy);
        gesture.apply(null, xxy);
    },
}
